package com.whut.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.EquipVerifyResultMapper;
import com.whut.mapper.EquipverificationsystemMapper;

import com.whut.model.EquipVerifyResult;
import com.whut.model.Equipverificationsystem;
import com.whut.service.EquipVerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 设备接口验证的一个service层的实现
 * @author 鲁佳磊
 * @create 2021-03-05 15:56
 */
@Service
public class EquipVerifyServiceImpl implements EquipVerifyService {

    @Autowired
    private EquipverificationsystemMapper equipverificationsystemMapper;

    @Autowired
    private EquipVerifyResultMapper equipVerifyResultMapper;

    /**
     * 因为有一些数据是合并的，所以不可以直接使用pageHelper来查询,每页两个分配
     * @param curPage
     * @param pageSize
     * @return
     */

    @Override
    public Object getAllEquipVerification(int curPage, int pageSize) {
        List<String> temp = equipverificationsystemMapper.getEquipTypeName();
        List<String> name = new ArrayList<>();
        for (int i = 0; i < temp.size(); i++) {
            if (i == 0) {
                name.add(temp.get(0));
                continue;
            }
            if (name.contains(temp.get(i))) {
                continue;
            } else {
                name.add(temp.get(i));
            }
        }
        List<Equipverificationsystem> equipverificationsystemList=new ArrayList<>();
        int allCount = equipverificationsystemMapper.getAllCount();
        for (int i = (curPage- 1)*pageSize; i<name.size() && i<curPage*pageSize ; i++) {
            equipverificationsystemList.addAll(equipverificationsystemMapper.getAllRecordByName(name.get(i)));
        }
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("list",equipverificationsystemList);
//      为了在分页器上面平均分配
        int totalSize=0;
        if (name.size()%pageSize==0){
            totalSize = equipverificationsystemList.size()*(name.size()/pageSize);
        }else{
            totalSize = equipverificationsystemList.size()*(name.size()/pageSize+1);
        }
        stringObjectHashMap.put("total",totalSize);
        stringObjectHashMap.put("pageSize",equipverificationsystemList.size());
        return stringObjectHashMap;
    }

    @Override
    public Object addEquipVerificationRecordStep1(String value) {
        try {
            Equipverificationsystem equipverificationsystem = new Equipverificationsystem();
            equipverificationsystem.setType(value);
            equipverificationsystem.setIsDisuse("N");
            equipverificationsystemMapper.insertSelective(equipverificationsystem);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    @Override
    public Object addEquipVerificationRecordStep2(String value, String id) {

        try {
            Equipverificationsystem temp = equipverificationsystemMapper.selectByPrimaryKey(Integer.parseInt(id));
//            如果是第一个数据那么直接修改这个数据
            if("".equals(temp.getLocation())||temp.getLocation()==null){
                temp.setLocation(value);
                equipverificationsystemMapper.updateByPrimaryKeySelective(temp);
            }else{
                Equipverificationsystem equipverificationsystem = new Equipverificationsystem();
                equipverificationsystem.setType(temp.getType());
                equipverificationsystem.setLocation(value);
                equipverificationsystem.setIsDisuse("N");
                equipverificationsystemMapper.insertSelective(equipverificationsystem);

            }
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    @Override
    public Object updateRecordDisuse(String id) {
        try {
            Equipverificationsystem equipverificationsystem1 = equipverificationsystemMapper.selectByPrimaryKey(Integer.parseInt(id));
            equipverificationsystem1.setIsDisuse("Y");
            equipverificationsystemMapper.updateByPrimaryKeySelective(equipverificationsystem1);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    @Override
    public Object deleteRecord(String id) {
        try {
            equipverificationsystemMapper.deleteByPrimaryKey(Integer.parseInt(id));
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    @Override
    public Object addEquipVerificationRecordStep3(String value, String id) {
        try {
            Equipverificationsystem equipverificationsystem1 = equipverificationsystemMapper.selectByPrimaryKey(Integer.parseInt(id));
            if(equipverificationsystem1.getDescription()!=null){
                Equipverificationsystem equipverificationsystem = new Equipverificationsystem();
                equipverificationsystem.setType(equipverificationsystem1.getType());
                equipverificationsystem.setLocation(equipverificationsystem1.getLocation());
                equipverificationsystem.setDescription(value);
                equipverificationsystem.setIsDisuse("N");
                equipverificationsystemMapper.insertSelective(equipverificationsystem);

            }else{
                equipverificationsystem1.setDescription(value);
                equipverificationsystemMapper.updateByPrimaryKeySelective(equipverificationsystem1);
            }
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    @Override
    public Object updateSystemRecord(Equipverificationsystem equipverificationsystem) {
        try {
            equipverificationsystemMapper.updateByPrimaryKeySelective(equipverificationsystem);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }

    @Override
    public Object getProjectName(String start,String end, String searchValue,String pageNum) {
//        使用分页插件来进行查询
        PageHelper.startPage(Integer.parseInt(pageNum), 6);
        List<Map<String,String>> list = equipVerifyResultMapper.getAllProjectName(start,end,searchValue);
        PageInfo result = new PageInfo(list);
        return result;
    }


    @Override
    public Object getAllVerifyResultByProjectId(String projectId) {
        List<EquipVerifyResult> equipVerifyResultList = equipVerifyResultMapper.getAllEquipVerifyResultByProjectId(projectId);
        return equipVerifyResultList;
    }
}
